package com.naihe.anderson.sms.dbhelper;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.naihe.anderson.sms.entity.Nhsms;
import com.naihe.anderson.sms.entity.NhsmsSession;

public class NhSmsDBHelper extends SQLiteOpenHelper {

	private static final int DEFAULT_VERSION = 1;
	private static final String DB_NAME_PREFIX = "nh_sms_db";
	public static final String NHSMS_TABLE_NAME = "nhsms";
	public static final String NHSMS_SESSION_TABLE_NAME = "nhsms_session";
	
	public final static String[] nhsms_projection_id = {
		Nhsms._ID,
		Nhsms.THREAD_ID
	};
	
	public final static String[] nhsms_projection = {
		Nhsms._ID,
		Nhsms.THREAD_ID,
		Nhsms.ADDRESS,
		Nhsms.PERSON,
		Nhsms.DATE,
		Nhsms.DATE_SENT,
		Nhsms.PROTOCOL,
		Nhsms.READ,
		Nhsms.STATUS,
		Nhsms.TYPE,
		Nhsms.REPLY_PATH_PRESENT,
		Nhsms.SUBJECT,
		Nhsms.BODY,
		Nhsms.SERVICE_CENTER,
		Nhsms.LOCKED,
		Nhsms.ERROR_CODE,
		Nhsms.SEEN
	};
	
	public final static String[] nhsms_session_projection = {
		NhsmsSession._TID,
		NhsmsSession.ADDRESS,
		NhsmsSession.PERSON,
		NhsmsSession.DATE,
		NhsmsSession.PROTOCOL,
		NhsmsSession.READ,
		NhsmsSession.BODY,
		NhsmsSession.NUM_SIZE,
		NhsmsSession.CIPHER
	};
	
	private final static String NHSMS_TABLE_CREATE  = "CREATE TABLE IF NOT EXISTS "
			+ NHSMS_TABLE_NAME
			+ "("
			+ Nhsms._ID 					+ " INTEGER PRIMARY KEY,"
			+ Nhsms.THREAD_ID 				+ " INTEGER,"
			+ Nhsms.ADDRESS 				+ " TEXT,"
			+ Nhsms.PERSON 					+ " INTEGER,"
			+ Nhsms.DATE 					+ " INTEGER,"
			+ Nhsms.DATE_SENT 				+ " INTEGER DEFAULT 0,"
			+ Nhsms.PROTOCOL 				+ " INTEGER,"
			+ Nhsms.READ 					+ " INTEGER DEFAULT 0,"
			+ Nhsms.STATUS 					+ " INTEGER DEFAULT -1,"
			+ Nhsms.TYPE 					+ " INTEGER,"
			+ Nhsms.REPLY_PATH_PRESENT 		+ " INTEGER,"
			+ Nhsms.SUBJECT 				+ " TEXT,"
			+ Nhsms.BODY 					+ " TEXT,"
			+ Nhsms.SERVICE_CENTER 			+ " TEXT,"
			+ Nhsms.LOCKED 					+ " INTEGER DEFAULT 0,"
			+ Nhsms.ERROR_CODE 				+ " INTEGER DEFAULT 0,"
			+ Nhsms.SEEN 					+ " INTEGER DEFAULT 0"
			+");";

	private final static String NHSMS_SESSION_TABLE_CREATE  = "CREATE TABLE IF NOT EXISTS "
			+ NHSMS_SESSION_TABLE_NAME
			+ "("
			+ NhsmsSession._TID 					+ " INTEGER PRIMARY KEY,"
			+ NhsmsSession.ADDRESS 					+ " TEXT,"
			+ NhsmsSession.PERSON 					+ " INTEGER,"
			+ NhsmsSession.DATE 					+ " INTEGER,"
			+ NhsmsSession.PROTOCOL 				+ " INTEGER,"
			+ NhsmsSession.NUM_SIZE 				+ " INTEGER,"
			+ NhsmsSession.READ 					+ " INTEGER DEFAULT 0,"
			+ NhsmsSession.BODY 					+ " TEXT,"
			+ NhsmsSession.CIPHER 					+ " TEXT"
			+");";
	
	public NhSmsDBHelper(Context context) {
		super(context, DB_NAME_PREFIX+".db", null, DEFAULT_VERSION);
	}
	
	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL(NHSMS_TABLE_CREATE);
		db.execSQL(NHSMS_SESSION_TABLE_CREATE);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

	}

	public SQLiteDatabase openForWrite(String name)  throws Exception{ 
		return getWritableDatabase();
	}

	public SQLiteDatabase openForRead(String name) throws Exception{
		return getReadableDatabase();
	}
	
}
